<template>
	<view class="store-wrap" v-if="inquiry">
		<u-search-sort placeholder="搜索商品" :title="categoryName" @confirm="searchGoods($event)" :keyword="goodsName"></u-search-sort>

		<scroll-view show-scrollbar="false" class="scroll-list" scroll-y @scrolltolower="scrollLoading()">
			<view class="scroll-list-wrap">
				<u-goods-item v-for="(item, index) in dataList" :key="index" :data="item"></u-goods-item>

				<uni-load-more v-if="dataTotalPage > 1" :status="dataLoading" :showText="false"></uni-load-more>
				<u-empty v-if="dataTotalPage == 0"></u-empty>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import uSearchSort from '../components/u-search-sort/u-search-sort.vue';
export default {
	components: {
		uSearchSort
	},
	data() {
		return {
			categoryName: '', //分类名称
			categoryCode: null, //分类编码
			goodsName: '', //搜索商品名
			

			pageNo: 1, //页码
			pageSize: 20, //页数
			dataTotalPage: 1, //数据总页数
			dataLoading: 'loading', //数据加载
			dataList: [] //数据列表
		};
	},
	onLoad(option) {
		this.categoryName = option.name ? option.name : '全部';
		this.categoryCode = option.code;
		this.goodsName = option.keyword;
	},
	onShow() {
		this.initData();
	},
	onHide() {},
	onUnload() {},
	methods: {
		/**
		 * 初始化数据
		 */
		initData() {
			this.pageNo = 1;
			this.dataList = [];
			this.dataTotalPage = 1;

			this.scrollLoading();
		},

		/**
		 * 滚动加载
		 */
		scrollLoading() {
			if (this.pageNo <= this.dataTotalPage) {
				this.goodsList();
			}
		},

		/**
		 * 搜索商品
		 */
		searchGoods(e) {
			this.goodsName = e.value;
			this.initData();
		},

		/**
		 * 查询商品列表
		 */
		goodsList() {
			let params = {
				pageNo: this.pageNo,
				pageSize: this.pageSize,
			};
			if (this.categoryCode) {
				params.code = this.categoryCode;
			}
			if (this.goodsName) {
				params.name = this.goodsName;
			}

			this.$api
				.goodsList({
					params: params
				})
				.then(res => {
					if (res.code == 200) {
						this.dataTotalPage = res.result.pages;

						if (this.pageNo == 1) {
							this.dataList = res.result.records;
						} else {
							this.dataList = this.dataList.concat(res.result.records);
						}

						if (res.result.current >= res.result.pages) {
							this.dataLoading = 'noMore';
						} else {
							this.dataLoading = 'loading';
						}
						this.pageNo++;
					}
				});
		}
	}
};
</script>

<style lang="less">
.store-wrap {
	.scroll-list {
		width: 750rpx;
		padding: 30rpx;
		box-sizing: border-box;
		height: calc(100vh - 120rpx);
		height: calc(100vh - 120rpx);
		.scroll-list-wrap {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
		}
	}
}
</style>
